package com.witdata.controller.subject;

import com.witdata.common.domain.Result;
import com.witdata.common.domain.Status;
import com.witdata.common.domain.dto.SqlCheckDto;
import com.witdata.common.domain.dto.SubjectDto;
import com.witdata.common.service.subject.SubjectService;
import com.witdata.common.util.IdUtils;
import com.witdata.mysql.domain.entity.TSqlCheck;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/subject")
public class SubjectController {
    private final SubjectService subjectService;
    private final IdUtils idUtils;

    @GetMapping("/list")
    public Result<List<SubjectDto>> getSubjectList(@RequestParam(name = "userId", required = false) Long userId) {
        List<SubjectDto> subjectList;
        if (userId == null) {
            subjectList = subjectService.getSubjectList();
        } else {
            subjectList = subjectService.getSubjectList(userId);
        }
        return Result.ok(Status.SUCCESS, subjectList);
    }

    @PostMapping("/create")
    public Result<String> createSubject(@RequestParam("name") String name, @RequestParam("avatar") MultipartFile avatar) {
        subjectService.createSubject(name, avatar);
        return Result.ok(Status.AddSuccess);
    }

    @GetMapping("/info")
    public Result<SubjectDto> getSubjectInfo(@RequestParam("id") Long id) {
        SubjectDto SubjectDto = subjectService.getSubjectInfoById(id);
        return Result.ok(Status.SUCCESS, SubjectDto);
    }

    @GetMapping("/remove")
    public Result<String> removeSubject(@RequestParam("id") Long id) {
        subjectService.removeSubject(id);
        return Result.ok(Status.SUCCESS);
    }

    @GetMapping("/sql/list")
    public Result<List<SqlCheckDto>> getSQLCheckList(@RequestParam("subjectId") Long subjectId) {
        List<SqlCheckDto> sqlCheckList = subjectService.getSQLCheckList(subjectId);
        return Result.ok(Status.SUCCESS, sqlCheckList);
    }
}
